﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SearchScreen.SearchScreenModule.Model;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Input;
using System.Threading.Tasks;
using SearchScreen.SearchScreenModule.Commands;
using System.Windows;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Prism.Events;
using SearchScreen.SDK.Infrastructure;

namespace SearchScreen.SearchScreenModule.ViewModel
{
    class RootDomainTypesViewModel : INotifyPropertyChanged
    {
        #region Members

        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<string> listings { get; private set; }

        #endregion

        #region Constructor

        public RootDomainTypesViewModel()
        {
            listings = new ObservableCollection<string>();
        }

        #endregion

        #region Properties

        DomainTypesViewModel _root;

        public DomainTypesViewModel Root
        {
            get
            {
                if (_root == null)
                {
                    _root = LoadRoot();
                    _root.OnIsCheckedChanged += Root_OnIsCheckedChanged;
                }
                return _root;
            }
        }

        void Root_OnIsCheckedChanged(object sender, EventArgs e)
        {
            this.UndoCommand.CanExecute = this.Root.IsChecked != false;
            this.SaveCommand.CanExecute = this.Root.IsChecked != false;
        }


        ObservableCollection<DomainTypesViewModel> _listDomainTypeViewModel;

        public ObservableCollection<DomainTypesViewModel> ListDomainTypeViewModel
        {
            get
            {
                if (_listDomainTypeViewModel == null)
                {
                    _listDomainTypeViewModel = new ObservableCollection<DomainTypesViewModel>() { this.Root };
                }
                return _listDomainTypeViewModel;
            }
        }


        #endregion

        #region Methods

        public static DomainTypesViewModel LoadRoot()
        {
            var xSer = new XmlSerializer(typeof(DealTree));
            var dealTree = (DealTree)xSer.Deserialize(File.OpenText(@"C:\Projects\SearchScreen\SearchScreen.SearchScreenModule\Assets\DealTreeSample.xml"));

            DomainTypesViewModel root = GenerateDealTree(dealTree);

            root.Initialize();

            return root;
        }

        private static DomainTypesViewModel GenerateDealTree(DealTree dealTree)
        {
            DomainTypesViewModel current = new DomainTypesViewModel(dealTree.name);
            if (dealTree.DealTreeChild != null)
            {
                foreach (var child in dealTree.DealTreeChild)
                {
                    current.Children.Add(GenerateDealTree(child));
                }
            }

            return current;
        }

        private void GetChecked(DomainTypesViewModel root)
        {
            foreach (DomainTypesViewModel child in root.Children)
            {
                if (child.Children != null)
                {
                    this.GetChecked(child);
                }
                if (child.IsChecked == true)
                {
                    this.listings.Add(child.HierarchicalName);
                }
            }
        }

        private void DisplayList()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in listings)
            {
                sb.Append(item);
                sb.AppendLine();
            }

            MessageBox.Show(sb.ToString());
        }

        #endregion

        #region Commands

        private Command _saveCommand;

        public Command SaveCommand
        {
            get
            {
                if (null == _saveCommand)
                {
                    _saveCommand = new Command(DoSave, false);
                }
                return _saveCommand;
            }
        }

        private void DoSave()
        {
            listings.Clear();
            this.GetChecked(this.Root);
            /*
            this.DisplayList();
            */
            var evt = ServiceLocator.Current.GetInstance<IEventAggregator>();
            evt.GetEvent<DomainTypesToCriteriaTupleEvent>().Publish(
                new DomainTypesToCriteriaTuplePayload(listings)
               );
        }
        
        private Command _undoCommand;

        public Command UndoCommand
        {
            get
            {
                if (null == _undoCommand)
                {
                    _undoCommand = new Command(DoUndo, false);
                }
                return _undoCommand;
            }
        }

        private void DoUndo()
        {
            this.Root.IsChecked = false;
        }

        #endregion

    }
}
